<?php 

/**
 * App's version of Zend_Controller_Action, implementing custom functionality.
 * All application controlers subclass this rather than Zend's version directly.
 *
 * @package App
 * @subpackage Controller
 */
class App_Controller_Action extends Zend_Controller_Action
{

    /**
     * A variable to hold the identity object
     *
     * @var object An instance of the user's identity or false
     */
    protected $_auth = false;

    /**
     * A variable to hold an identify of the user
     *
     * Will be !false if there is a valid identity
     *
     * @var object An instance of the user's identity or false
     */
    protected $_identity = false;

    /**
    * A variable to hold an instance of the bootstrap object
    *
    * @var object An instance of the bootstrap object
    */
    protected $_bootstrap;

    /**
    * A variable to hold an instance of the logger object
    *
    * @var App_Log An instance of the logger object
    */
    protected $_logger = null;

    /**
     * A variable to hold the mailer
     *
     * @var object An instance of the mailer
     */
    protected $_mailer = null;

    /**
     * A variable to hold the session namespace
     *
     * @var object An instance of the session namespace
     */
    protected $_session = null;
    
    /**
     * A variable to hold the messenger
     * 
     * @var App_Controller_Action_Helper_Messenger
     */
    protected $_messenger = null;

    /**
     * A variable to hold the Doctrine manager
     *
     * @var Doctrine\ORM\EntityManager An instance of the entity manager
     */
    protected $_entityManager = null;

    /**
     * @var array an array representation of the application.ini
     */
    protected $_options = null;

	/** 
	 * set active for current module and action
	 */
	public function init() {
		parent::init();
	}
    
    /**
     * Override the Zend_Controller_Action's constructor (which is called
     * at the very beginning of this function anyway).
     *
     * @param object $request See Parent class constructor
     * @param object $response See Parent class constructor
     * @param object $invokeArgs See Parent class constructor
     */
    public function __construct(Zend_Controller_Request_Abstract  $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = null)
    {
    	// call the parent's version where all the Zend magic happens
        parent::__construct( $request, $response, $invokeArgs );
        // get the bootstrap object
        $this->_bootstrap = $invokeArgs['bootstrap'];

        // load up the options
        $this->_options = $this->_bootstrap->getOptions();

        // and from the bootstrap, we can get other resources:
        $this->_logger   = $this->_bootstrap->getResource( 'logger' );
        $this->_session  = $this->_bootstrap->getResource( 'namespace' );
        $this->_messenger  = Zend_Controller_Action_HelperBroker::getStaticHelper('messenger');
        $this->_entityManager = $this->_bootstrap->getResource( 'doctrine' );
        $this->_auth     = $this->_bootstrap->getResource( 'auth' );
        $this->_identity = $this->_auth->getIdentity();
		
        try
        {
            $this->view->session     = $this->_session;
            $this->view->options     = $this->_options;
            $this->view->auth        = $this->_auth;
            $this->view->hasIdentity = $this->_auth->hasIdentity();
            $this->view->identity    = $this->_identity;
        }
        catch( Zend_Exception $e )
        {
            echo _( 'Caught exception' ) . ': ' . get_class( $e ) . "\n";
            echo _( 'Message' ) . ': ' . $e->getMessage() . "\n";
        }
		
        $this->view->addHelperPath( 'App/View/Helper', 'App_View_Helper' );

        $this->view->controller = $this->getRequest()->getParam( 'controller' );
        $this->view->action     = $this->getRequest()->getParam( 'action'     );

        // if we issue a redirect, we want it to exit immediatly
        $this->getHelper( 'Redirector' )->setExit( true );
    }
    
    /**
     * A utility method to get a named resource.
     *
     * @param string $resource
     */
    public function getResource( $resource )
    {
        return $this->_bootstrap->getResource( $resource );
    }

    /**
     * Returns the logger object
     *
     * @return App_Log object
     */
    public function getLogger()
    {
        return $this->_logger;
    }

    /**
     * Return the Zend_Auth instance.
     * @return Zend_Auth The Zend_Auth instance or false
     */
    protected function getAuth()
    {
        return $this->_auth;
    }


    /**
     * Returns the identify object for the Zend_Auth session.
     *
     * Will be !false if there is a valid identity
     *
     * @return array The Zend_auth identity object or false
     */
    protected function getIdentity()
    {
        return $this->_identity;
    }


    /**
     * Get the user ORM object.
     *
     * Returns the instance of the Doctrine User object for the logged in user.
     *
     * @return Admin The admin object or false.
     */
    protected function getAdmin()
    {
        return $this->_admin;
    }


    /**
     * Get the namespace (session).
     *
     * @return Zend_Session_Namespace The session namespace.
     */
    protected function getSession()
    {
        return $this->_session;
    }


    /**
     * A function to generate a URL with the given parameters.
     *
     * This is a useful function as no knowledge of the application's path is required.
     *
     * @param string|bool $controller The controller to call.
     * @param string|bool $action     The action to call (controller must be set if setting action)
     * @param string|bool $module      The module to use. Set to false to ignore.
     * @param string|bool $params     An array of key value pairs to add to the URL.
     */
    public function genUrl( $controller = false, $action = false, $module = false, $params = array() )
    {
        $url = $this->getFrontController()->getBaseUrl();

        // when the webpage is directly under "xyz.com/", and not in "xyz.com/wherever"
        // an empty href attribute in an anchor tag means "the current URL", which is not always good
        if ($url == '')
        {
            $url = 'http';
            if ( ( isset($_SERVER['HTTPS']) ) && ( $_SERVER['HTTPS'] == 'on' ) ) $url .= 's';
            $url .= "://{$_SERVER['HTTP_HOST']}";
        }

        if( $module )
            $url .= "/{$module}";

        if( $controller )
            $url .= "/{$controller}";

        if( $action )
            $url .= "/{$action}";

        foreach( $params as $var => $value )
            $url .= "/{$var}/{$value}";

        return $url;
    }


    /**
     * A generic authorisation checker for use in the controllers.
     *
     * Checks if the user:
     *    - is logged in
     *    - is a superadmin or, if not:
     *    -  is allowed to access a specific domain.
     *
     * This function performs a _redirect() (and die()) if the authisation conditions are not met.
     *
     * @param boolean $super Set to true to check if the user is a super admin
     * @param boolean $redirect default true true if we want to redirect the user, false if we want a boolean return value
     * @return void
     */
    protected function authorise( $super = false, $redirect = true )
    {
    	return true;
    }
}
